﻿/*
  Project:       DataAnalyse
  File:          UserHandler.ashx
  Description:   
  Author:        Lanny(damon_lan@163.com)
  Date:          Sept,2014
  version:       2.0
  Modify:
  
  
  
  References:

 
                 
 Comments:
 * Reconstruct the UserHandler function. Merge 4 old files into 1 file.
 * Add log function.


Notes:
 * --------------UserManager.aspx.cs construct function don't complete.

*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DataAnalyseTool.IDAL;
using DataAnalyseTool.Model;
using DataAnalyseTool.BusinessTier.Bll;
using System.Text;
using System.Data;
using DataAnalyseTool.Utilities;

namespace DataAnalyseTool.BusinessTier.Handlers
{
  /// <summary>
  /// Summary description for UserHandler
  /// </summary>
  public class UserHandler : IHttpHandler, IRepositoryHandler
  {
    protected static readonly log4net.ILog log =
    log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    UserBLL userBll = new UserBLL();

    public void ProcessRequest(HttpContext context)
    {
      context.Response.ContentType = "text/plain";

      string type = context.Request.Params["action"];
      switch (type)
      {
        case "Add":
          AddEntity(context);
          break;
        case "Delete":
          DeleteEntity(context);
          break;
        case "Update":
          UpdateEntity(context);
          break;
        case "Get":
          GetEntity(context);
          break;
        default:
          log.Error("Get the Type value occur exception, the Type value is: " + type);
          break;
      }
    }

    public void AddEntity(HttpContext ctx)
    {
      Users user = new Users();
      try
      {
        user.UserName = ctx.Request["UserName"].ToString();
        user.Password = ctx.Request["Password"].ToString();
        user.IsAdmin = Convert.ToByte(ctx.Request["IsAdmin"].ToString() == "true" ? "1" : "0");
        string qq = ctx.Request.Params["QQ"].ToString();
        string wangwang = ctx.Request.Params["Wangwang"].ToString();
        string zhifubaoaccount = ctx.Request.Params["ZhifubaoAccount"].ToString();
        string zhifubaoname = ctx.Request.Params["ZhifubaoName"].ToString();
        string rebatezhifubao = ctx.Request.Params["RebateZhifubao"].ToString();
        string rebatezhifubaoname = ctx.Request.Params["RebateZhifubaoName"].ToString();
        string caifutongaccount = ctx.Request.Params["CaifutongAccount"].ToString();
        string caifutongname = ctx.Request.Params["CaifutongName"].ToString();
        string shopid = ctx.Request.Params["ShopID"].ToString();
        string cash = ctx.Request.Params["Cash"].ToString();
        string cellphone = ctx.Request.Params["Cellphone"].ToString();
        string fixphone = ctx.Request.Params["Fixphone"].ToString();
        string receivename = ctx.Request.Params["ReceiveName"].ToString();
        string receiveaddress = ctx.Request.Params["ReceiveAddress"].ToString();
        string receivecellphone = ctx.Request.Params["ReceiveCellphone"].ToString();
        string receivefixphone = ctx.Request.Params["ReceiveFixphone"].ToString();
        string date = ctx.Request.Params["Date"].ToString();

        user.ContainFileds = user.ContainFileds = "\"QQ\":\"" + qq + "\",\"Wangwang\":\"" + wangwang + "\"," +
            "\"ZhifubaoAccount\":\"" + zhifubaoaccount + "\",\"ZhifubaoName\":\"" + zhifubaoname + "\"," +
            "\"RebateZhifubao\":\"" + rebatezhifubao + "\",\"RebateZhifubaoName\":\"" + rebatezhifubaoname + "\"," +
            "\"CaifutongAccount\":\"" + caifutongaccount + "\",\"CaifutongName\":\"" + caifutongname + "\"," +
            "\"ShopID\":\"" + shopid + "\",\"Cash\":\"" + cash + "\",\"Cellphone\":\"" + cellphone + "\"," +
            "\"Fixphone\":\"" + fixphone + "\",\"ReceiveName\":\"" + receivename + "\"," +
            "\"ReceiveAddress\":\"" + receiveaddress + "\",\"ReceiveCellphone\":\"" + receivecellphone + "\"," +
            "\"ReceiveFixphone\":\"" + receivefixphone + "\",\"Date\":\"" + date + "\"";

        if (userBll.GetUser(user.UserName) < 1)
        {
          userBll.AddUser(user);
          string returnValue = "{UserName:'" + user.UserName + "',Password:'" + user.Password + "',IsAdmin:'" + user.IsAdmin + "',ContainsProFileds:'" + user.ContainFileds + "'}";
          ctx.Response.Write(returnValue);
          return;
        }
        ctx.Response.Write("{Repeater:'1'}");
      }
      catch (Exception ex)
      {
        log.Error("Add users occur exception, exceptions is: " + ex.ToString());
      }

    }

    public void DeleteEntity(HttpContext ctx)
    {

      string id = ctx.Request.Params["id"].ToString();

      if (userBll.DeleteUser(id) == 1)
      {

        ctx.Response.Write("{'success':true}");
        return;
      }

      ctx.Response.Write("{'success':'false'}");
    }

    public void UpdateEntity(HttpContext ctx)
    {
      Users user = new Users();
      user.Id = ctx.Request.Params["id"].ToString();
      user.UserName = ctx.Request.Params["UserName"].ToString();
      user.Password = ctx.Request.Params["Password"].ToString();
      user.IsAdmin = Convert.ToByte(ctx.Request.Params["IsAdmin"].ToString() == "true" ? "1" : "0");


      string qq = ctx.Request.Params["QQ"].ToString();
      string wangwang = ctx.Request.Params["Wangwang"].ToString();
      string zhifubaoaccount = ctx.Request.Params["ZhifubaoAccount"].ToString();
      string zhifubaoname = ctx.Request.Params["ZhifubaoName"].ToString();
      string rebatezhifubao = ctx.Request.Params["RebateZhifubao"].ToString();
      string rebatezhifubaoname = ctx.Request.Params["RebateZhifubaoName"].ToString();
      string caifutongaccount = ctx.Request.Params["CaifutongAccount"].ToString();
      string caifutongname = ctx.Request.Params["CaifutongName"].ToString();
      string shopid = ctx.Request.Params["ShopID"].ToString();
      string cash = ctx.Request.Params["Cash"].ToString();
      string cellphone = ctx.Request.Params["Cellphone"].ToString();
      string fixphone = ctx.Request.Params["Fixphone"].ToString();
      string receivename = ctx.Request.Params["ReceiveName"].ToString();
      string receiveaddress = ctx.Request.Params["ReceiveAddress"].ToString();
      string receivecellphone = ctx.Request.Params["ReceiveCellphone"].ToString();
      string receivefixphone = ctx.Request.Params["ReceiveFixphone"].ToString();
      string date = ctx.Request.Params["Date"].ToString();


      user.ContainFileds = "\"QQ\":\"" + qq + "\",\"Wangwang\":\"" + wangwang + "\"," +
          "\"ZhifubaoAccount\":\"" + zhifubaoaccount + "\",\"ZhifubaoName\":\"" + zhifubaoname + "\"," +
          "\"RebateZhifubao\":\"" + rebatezhifubao + "\",\"RebateZhifubaoName\":\"" + rebatezhifubaoname + "\"," +
          "\"CaifutongAccount\":\"" + caifutongaccount + "\",\"CaifutongName\":\"" + caifutongname + "\"," +
          "\"ShopID\":\"" + shopid + "\",\"Cash\":\"" + cash + "\",\"Cellphone\":\"" + cellphone + "\"," +
          "\"Fixphone\":\"" + fixphone + "\",\"ReceiveName\":\"" + receivename + "\"," +
          "\"ReceiveAddress\":\"" + receiveaddress + "\",\"ReceiveCellphone\":\"" + receivecellphone + "\"," +
          "\"ReceiveFixphone\":\"" + receivefixphone + "\",\"Date\":\"" + date + "\"";


      userBll.UpdateUser(user);
      string returnValue = "{id:'" + user.Id + "',UserName:'" + user.UserName + "',Password:'" + user.Password + "',IsAdmin:'" + user.IsAdmin + "',ContainsProFileds:'" + user.ContainFileds + "'}";
      ctx.Response.Write(returnValue);
    }

    public void GetEntity(HttpContext ctx)
    {
      try
      {
        StringBuilder sbRet = new StringBuilder();
        DataTable dt = userBll.GetAllUser();
        DataRow dr = dt.NewRow();
        dr["UserID"] = "10000";
        dr["UserName"] = "选择用户";
        dt.Rows.InsertAt(dr, 0);

        dt.Columns.Add("id");
        for (int i = 1; i < dt.Rows.Count; i++)
        {
          dt.Rows[i]["id"] = dt.Rows[i]["UserId"];
        }

        sbRet.Append(Utility.GetJsonString(dt));

        ctx.Response.Write(sbRet);
      }
      catch (Exception ex)
      {
        log.Error("Get the users occur exception, exception information : " + ex.ToString());
      }

    }

    public bool IsReusable
    {
      get
      {
        return false;
      }
    }
  }
}